package com.lamda.worker.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.util.List;


/**
 * Http请求工具
 */
public class HttpUtils {

    /**
     * 无效的HTTP响应码
     */
    public static final int INVALID_HTTP_STATUS_CODE = -1;


    public static String get( String url) throws Exception {
        return get(url,null);
    }

    /**
     * @param httpClient
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String get(HttpClient httpClient, String url, List<NameValuePair> params) throws Exception {


        String body = null;
        // Get请求
        HttpGet httpget = new HttpGet(url);

        // 设置参数
        URI uri = new URI(httpget.getURI().toString());
        if (params != null) {
            String str = EntityUtils.toString(new UrlEncodedFormEntity(params));
            uri = new URI(httpget.getURI().toString() + "?" + str);
        }

        httpget.setURI(uri);

        // 发送请求
        HttpResponse httpresponse = httpClient.execute(httpget);

        // 获取返回数据
        HttpEntity entity = httpresponse.getEntity();

        body = EntityUtils.toString(entity);

        EntityUtils.consume(entity);

        httpget.releaseConnection();

        return body;

    }


    public static String get(String url, List<NameValuePair> params) throws Exception {
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        HttpClient httpClient = httpClientBuilder.build();
        return get(httpClient, url, params);
    }


    public static String post(String url, List<NameValuePair> params) throws Exception {
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        HttpClient httpClient = httpClientBuilder.build();
        return post(httpClient, url, params);
    }

    /**
     * @param httpClient
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String post(HttpClient httpClient, String url, List<NameValuePair> params) throws Exception {

        String body = null;

        // Post请求

        HttpPost httppost = new HttpPost(url);
        // 设置参数
        URI uri = new URI(httppost.getURI().toString());
        if (params != null) {
            String str = EntityUtils.toString(new UrlEncodedFormEntity(params));
            uri = new URI(httppost.getURI().toString() + "?" + str);
        }

        httppost.setURI(uri);
        // 发送请求

        HttpResponse httpresponse = httpClient.execute(httppost);

        // 获取返回数据

        HttpEntity entity = httpresponse.getEntity();

        body = EntityUtils.toString(entity);

        EntityUtils.consume(entity);

        httppost.releaseConnection();

        return body;

    }


    public static HttpResponse getReturnHttpResponse(String url, List<NameValuePair> params) throws Exception {
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        HttpClient httpClient = httpClientBuilder.build();

        return getByReturnHttpResponse(httpClient, url, params);
    }


    public static HttpResponse getByReturnHttpResponse(HttpClient httpClient, String url, List<NameValuePair> params) throws Exception {

        // Get请求
        HttpGet httpget = new HttpGet(url);

        // 设置参数
        URI uri = new URI(httpget.getURI().toString());
        if (params != null) {
            String str = EntityUtils.toString(new UrlEncodedFormEntity(params, "UTF-8"));
            uri = new URI(httpget.getURI().toString() + "?" + str);
        }

        httpget.setURI(uri);

        // 发送请求
        HttpResponse httpresponse = httpClient.execute(httpget);
        httpget.releaseConnection();
        return httpresponse;
    }

    public static void consumeHttpHttpEntity(HttpEntity httpEntity) {
        try {
            EntityUtils.consume(httpEntity);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
